﻿using Application.Common.Utils;
using Infrastructure.Persistence.Repository;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Application.Business.TestSimsilary
{
    public class GetSimilarySummaryAction
    {
        private JieBa jb = new();
        private Shingling sl = new();
        private SummaryRepository _summaryRepository;
        public GetSimilarySummaryAction(SummaryRepository summaryRepository)
        {
            _summaryRepository = summaryRepository;
        }
        public async Task<string> start(string text)
        {
            var everyValue = await _summaryRepository.GetSummarySimilaries(1,1000);

            
            Simhash sm = new();
            var curDic = jb.Cut(sl.scrub(text));
            sm.GenerateSimhash(curDic);
            var value = sm.value;
            List<(int id, int distance)> everyDis = new();
            var sw = new Stopwatch();
            sw.Start();
            foreach (var it in everyValue)
            {
                var dis = jb.GetDistance(value, it.value);
                everyDis.Add((it.sid, dis));
            }
            sw.Stop();
            var lastest = everyDis.OrderBy(s => s.distance).First();
            return $"最相似文章id：{lastest.id};比对最近距离：{lastest.distance}; 比对文章数量：{everyValue.Count()};此次比对耗时：{sw.ElapsedMilliseconds} ms";
        }

        /// <summary>
        /// 百万数据  亿 效率测试
        /// </summary>
        public async void Test()
        {
            int i = 1;
            Random r = new();
            Stopwatch sw = new();
            List<string> tests = new();
            List<(string value, int distance)> everyDis = new();

            var everyValue = await _summaryRepository.GetSummarySimilaries(1, 1000);
            while (i++ <= 100000)
            {
                foreach (var it in everyValue)
                {
                    tests.Add(it.value);
                }
            }

            int j = 1;
            sw.Start();
            foreach (var it in tests)
            {
                j++;
                if(j==10000 || j == 100000|| j == 1000000 || j == 10000000 || j==100000000)
                    Console.WriteLine($"j={j},time={sw.ElapsedMilliseconds}ms");
                var dis = jb.GetDistance(12243962506111827908, it);
                everyDis.Add((it, dis));
            }
            Console.WriteLine();

        }
    }
}
